#include"queue.h"
void QueueInit(que* qp)
{
	assert(qp);
	qp->head = qp->tail = NULL;
	qp->size = 0;
}
void QueuePush(que* qp, Datatype x)
{
	assert(qp);
	node* newnode = (node*)malloc(sizeof(node));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (qp->size == 0)
	{
		qp->head = newnode;
		qp->tail = newnode;
	}
	else
	{
		qp->tail->next = newnode;
		qp->tail = newnode;
	}
	qp->size++;
}
void QueuePop(que* qp)
{
	assert(qp);
	assert(!QueueEmpty(qp));
	if (qp->head == NULL)
	{
		free(qp->head);
		qp->head = qp->tail = NULL;
	}
	else
	{
		node* cur = qp->head->next;
		free(qp->head);
		qp->head = cur;
	}
	qp->size--;
}
bool QueueEmpty(que* qp)

{
	assert(qp);
	return qp->head == NULL;
}
int  QueueSize(que* qp)
{
	assert(qp);
	return qp->size;
}
Datatype QueueFront(que* qp)
{
	assert(qp);
	assert(!QueueEmpty(qp));
	return qp->head->data;
}
Datatype QueueBehind(que* qp)
{
	assert(qp);
	assert(!QueueEmpty(qp));
	return qp->tail->data;
}

void QueueDestroy(que* qp)
{
	assert(qp);
	node* cur = qp->head;
	while (cur)
	{
		node* node = cur->next;
		free(cur);
		cur = node;
	}
	qp->head = qp->tail = NULL;
	qp->size = 0;
}
void Queueprint(que* qp)
{
	assert(qp);
	node* cur = qp->head;
	while (cur)
	{
		printf("%d", cur->data);
		cur = cur->next;
	}
}